<?php

class Project_m extends CI_Model{
	
	function __construct(){
		parent::__construct();
		$this->load->model('user');
	}
	
	function save_project(){
		
		$data = array(
			'project_name'=>$this->input->post('pname'),
			'coordinates'=>$this->input->post('coor')
		);
		
		 return $this->db->insert('project',$data);
		
	}// end function save projecct..

	function search(){
		$q = "select * from project where project_name LIKE '%".$this->input->post('projectsearch')."%'";
		
		$res = $this->db->query($q);
		
		return ($res->num_rows() > 0) ? $res->result():false;
	}
	function display_all(){
		$q = "select * from project";
		$res = $this->db->query($q);
		
		return ($res->num_rows() > 0) ? $res->result():false;
	}
	// project management..
	function addnew_project(){
		
		
		$user = $this->user->get_user_info();
		
		// uploading the icon file.. 
		$fileext = $_FILES['project_icon']['name'];
		$ext = substr(strrchr($fileext, '.'), 1); 
		$filename = 'client_'.$user->id.'_'.str_replace(' ', '',$this->input->post('project_name')).'.'.$ext;
		
		$config['upload_path'] = './clients/images';
		$config['allowed_types'] = 'ico|jpg|png';
		$config['max_size']	= '100';
		$config['max_width']  = '35';
		$config['max_height']  = '60';
		$config['file_name'] = $filename;
		
		$this->upload->initialize($config);
		// loading the configuration.. 
	
		
		if ( ! $this->upload->do_upload('project_icon'))
		{
			$error = array('error' => $this->upload->display_errors());
			$filename = "default.png";
			
		}
		
			
			$project = array(
			'client_id' => $user->id,
			'project_name' => $this->input->post('project_name'),
			'lat' => $this->input->post('lat'),
			'lng' => $this->input->post('lng'),
			'comp_address' => $this->input->post('comp_address'),
			'project_icon' => 'clients/images/'.$filename,
			'company_name' => $this->input->post('company_name'),
			'comp_inv_name' => $this->input->post('comp_inv_name'),
			'afe_po_num' => $this->input->post('afe_po_num'),
			'p_manager' => $this->input->post('p_manager'),
			'pm_phone1' => $this->input->post('pm_phone1'),
			'pm_phone2' => $this->input->post('pm_phone2'),
			'pm_email' => $this->input->post('pm_email'),
			'second_contact_name' => $this->input->post('second_contact_name'),
			'sc_phone1' => $this->input->post('sc_phone1'),
			'sc_phone2' => $this->input->post('sc_phone2'),
			'sc_email' => $this->input->post('sc_email'),
			'project_desc' => $this->input->post('project_desc'),
			'project_start_date' => $this->input->post('project_start_date'),
			'project_end_date' => $this->input->post('project_end_date'),
			);
			//adding office..
			$office_res = $this->db->insert('project',$project); // inserting office info..
			
			return ($office_res) ? true:var_dump(mysql_error());
		

	}
	function save_edit(){ // save edit project
		$client_id = $this->uri->segment(3);// client id..
		$project_id = $this->uri->segment(4); // project id..
		$user = $this->user->get_user_info();
		if($client_id == false || $project_id == false){
			redirect('project/view');
		}// if uri dont have any value redierect.. 
		if($client_id != $user->id){
			redirect('project/view');
		} // if session and client id uri is not equal rederect..
	
		
			
				// uploading the icon file.. 
			$fileext = $_FILES['project_icon']['name'];
			$ext = substr(strrchr($fileext, '.'), 1); 
			$filename = 'client_'.$user->id.'_'.str_replace(' ', '',$this->input->post('project_name')).'.'.$ext;
			 $config['overwrite'] = TRUE; //overwrite user avatar
			$config['upload_path'] = './clients/images';
			$config['allowed_types'] = 'ico|jpg|png';
			$config['max_size']	= '100';
			$config['max_width']  = '35';
			$config['max_height']  = '60';
			$config['file_name'] = $filename;
			
			$this->upload->initialize($config);
			// loading the configuration.. 
			$upload = $this->upload->do_upload('project_icon');
	
		
			$project = array(
			'project_name' => $this->input->post('project_name'),
			'lat' => $this->input->post('lat'),
			'lng' => $this->input->post('lng'),
			'comp_address' => $this->input->post('comp_address'),
			'project_icon' => 'clients/images/'.$filename,
			'company_name' => $this->input->post('company_name'),
			'comp_inv_name' => $this->input->post('comp_inv_name'),
			'afe_po_num' => $this->input->post('afe_po_num'),
			'p_manager' => $this->input->post('p_manager'),
			'pm_phone1' => $this->input->post('pm_phone1'),
			'pm_phone2' => $this->input->post('pm_phone2'),
			'pm_email' => $this->input->post('pm_email'),
			'second_contact_name' => $this->input->post('second_contact_name'),
			'sc_phone1' => $this->input->post('sc_phone1'),
			'sc_phone2' => $this->input->post('sc_phone2'),
			'sc_email' => $this->input->post('sc_email'),
			'project_desc' => $this->input->post('project_desc'),
			'project_start_date' => $this->input->post('project_start_date'),
			'project_end_date' => $this->input->post('project_end_date'),
			);
		
			if($upload == false){
				unset($project['project_icon']);
				
			}
			
			//edit project..
			$where = "client_id = '".$user->id."' and id = '$project_id'";
			
			$this->db->where($where);
		
			$office_res = $this->db->update('project',$project); // inserting project info..
			
			return ($office_res) ? true:var_dump(mysql_error());
		
			
	}// end save edit project
	function project_list(){
			$user = $this->user->get_user_info();
			
			$this->db->where('client_id',$user->id);
			$res = $this->db->get('project');
			return ($res->num_rows() > 0) ? $res->result():false;
		
	}
	function get_project(){
	
			$client_id = $this->uri->segment(3);// client id..
			$project_id = $this->uri->segment(4); // project id..
			$user = $this->user->get_user_info();
			
		if($client_id == $user->id){	
		
			if($client_id != false || $project_id != false){
			
				$this->db->where('client_id',$user->id);
				$this->db->where('id',$project_id);
				$res = $this->db->get('project');
				return ($res->num_rows() > 0) ? $res->row():false; 
			
			}
			else{
				redirect('project/view');
			}
		}
		else{
			redirect('project/view');
		}
			
		
	}
function datatables_pr(){
			$user = $this->user->get_user_info();
			$client_id = $user->id;
			$sEcho = $this->input->get('sEcho');//to get the url value
			$sqlCount = "select count(*) as xcount from project";
			$count = $this->db->query($sqlCount)->result();
			foreach($count as $c){
			$num = $c->xcount;
			}
		  
		   
			$sqlInfo = "Select
						* 
						from 
						project
						where client_id = '$client_id'
						";
		  
			$queries = $this->db->query($sqlInfo)->result();
		  
	
		  
		  $numRows = count($queries);
		  
		  $queries  = array(
		      'sEcho' => '1',//$sEcho,
		      'iTotalRecords' => $num,//total number of records in the db
		      'iTotalDisplayRecords'=> $num,//total number of records after filtering
		      'aaData' => $queries
		     
		        );
		  
		  return json_encode( $queries );
			
			
} // end function datatables.. 
	
}// end project model